package com.skt.common_hr.biztrip_sac.model
{
	import com.skt.common_hr.biztrip_sac.model.vo.BizTripPlaceVO;
	import com.skt.common_hr.biztrip_sac.view.components.DailyBizTrip;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.approval.IRequestApprovalProxy;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;

	public class DailyApprovalProxy extends CommonProxy implements IRequestApprovalProxy
	{
		public static const NAME:String = "DailyApprovalProxy";
		public var dataProvider:ArrayCollection;
		public var amount:String;
		
		public var sEduChk:String;
		
		//0609
		public var placeList:ArrayCollection;
		public var adjustmentTable:Array;
		
		private var _bAppType:Boolean = false;		// 교육이수 체크
		
		
		private var templateHTML:String = "";
		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function DailyApprovalProxy(data:Object=null)
		{
			super(NAME, data);
			loadTemplate();
		}

		private var _key:String = "";
		public function get key():String
		{
			return _key;
		}
		public function set key(k:String):void
		{
			_key = k;
		}
		
		public function set appType(b:Boolean):void
 		{
 			_bAppType = b;
 		}

		public function get title():String
		{
			var tmp:String = "시내출장 (";
			
			if(_bAppType) {
				tmp = "시내교육출장 (";
			}else {
				tmp = "시내출장 (";
			}
			
			tmp += getTerm();
			tmp += ", 출장비 : " + amount + "원)";
			return tmp;
		}

		public function get content():String
		{
			return buildHTML(dataProvider);
		}

		public function get appCode():String
		{
			return "04"; 	//
		}

 		private function getTerm():String
 		{
 			var begda:Date = null;
 			var endda:Date = null;

			for each(var vo:BizTripPlaceVO in dataProvider) {
				if(begda==null || Number(vo.BEGDA) < Number(begda)) begda = vo.BEGDA;
				if(endda==null || Number(vo.ENDDA) > Number(endda)) endda = vo.ENDDA;
			}
			if(Number(begda) == Number(endda)) {
				return DateUtil.toDefaultString(begda);
			}
			else
				return DateUtil.toDefaultString(begda) + " ~ " + DateUtil.toDefaultString(endda);
 		}

		private function loadTemplate():void
		{
			var request:URLRequest = new URLRequest("assets/template/BizTripDailySac.tmp");
			loader.addEventListener(Event.COMPLETE, templateResult);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}


		private function templateResult(event:Event):void
		{
			templateHTML = loader.data;
		}

		private function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("ioErrorHandler: " + event);
		}



		private function buildHTML(dp:ArrayCollection):String
		{
			var htmlAll:String = templateHTML;
			var htmlBlock:String = "";

			var ptnCname:RegExp  = /__MEMBER__/;
			var ptnRecordBlock:RegExp = /__RECORD_BLOCK__/;
			var ptnAmount:RegExp = /__AMOUNT__/;
			
			//0609
			var ptnPlaceNewBlock:RegExp = /__placeNew__/;
			var ptnTitle:RegExp  = /__TITLE__/;
			var ptnTerm:RegExp = /__TERM__/;
			var ptnPurpose:RegExp= /__PURPOSE__/;
			
			var ptnEduChk:RegExp = /__EDUCHK__/;


			htmlAll = htmlAll.replace(ptnTitle, adjustmentTable[0].TRTITLE);
			htmlAll = htmlAll.replace(ptnPurpose, adjustmentTable[0].PURPOSE);
			htmlAll = htmlAll.replace(ptnTerm, tripTerm());
												
			htmlAll = htmlAll.replace(ptnCname, Variables.myName+"("+Variables.myPernr+")");
			htmlAll = htmlAll.replace(ptnRecordBlock, buildHtmlRecords(dp));
//shgoo : 2009.06.25  임시저장 없이 승인요청 클릭시 승인요청 화면에서 정산금액  이전값이 표시되는 오류 수정
			//htmlAll = htmlAll.replace(ptnAmount, NumberUtil.NumberFormat(view.dailyCost));
			htmlAll = htmlAll.replace(ptnAmount, amount);
			
			//0609
			htmlAll = htmlAll.replace(ptnPlaceNewBlock, buildPlaceNewBlock());
///////////////////////


			if(_bAppType){
				htmlAll = htmlAll.replace(ptnEduChk, '시내교육출장');
			}else {
				htmlAll = htmlAll.replace(ptnEduChk, '시내출장');	
			}

			return htmlAll;
		}

		private function buildHtmlRecords(dp:ArrayCollection):String
		{
			var blocks:String = "";
			var tempRecord:String = "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' height=30><P>";

			for each(var vo:BizTripPlaceVO in dp) {
				blocks += "<TR>" + tempRecord + DateUtil.toDefaultString(vo.BEGDA) + "</P></TD>";
				blocks += tempRecord + vo.BREN + "</P></TD>";
			}
			return blocks
		}
		
		private function buildPlaceNewBlock():String
		{
			var blocks:String = "";
			var openColumn:String = "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;'><P>&nbsp;";

			for each(var vo:BizTripPlaceVO in placeList) {
				blocks += "<TR>" + openColumn + DateUtil.toDefaultString(vo.BEGDA) + "&nbsp;</P></TD>";	// 출장일자
				blocks += openColumn + vo.BREN + " ~ " + vo.BREN1 + "&nbsp;</P></TD>";
				blocks += openColumn + vo.TGBNT + "&nbsp;</P></TD>";
				blocks += openColumn + vo.CNUM + "&nbsp;</P></TD>";
				blocks += openColumn + vo.DRIKM + "&nbsp;</P></TD>";
				blocks += "</TR>";
			}
			return blocks
		}		

		private function tripTerm():String
		{
			//return DateUtil.toDefaultString(adjustmentTable[0].BEGDA) + " ~ " + DateUtil.toDefaultString(adjustmentTable[0].ENDDA);
			return DateUtil.toDefaultString(adjustmentTable[0].BEGDA);
		}
	}
}